// import CommonHook from "@/hook/commonHook";
import { message, Modal } from "ant-design-vue";
import { Ref, computed, createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { deletePaymentWaterApi, getFinancePaymentSerialExcelListApi, getListApi, getPageStaticApi } from "@/api/financialManagement/payableManagement/paymentRegistration";
import dayjs from "dayjs";
export default function OperationHook(state: {
  searchForm: PaymentRegistration.SearchForm;
  tableData: Ref<PaymentRegistration.ListRows[]>;
  tableRef: Ref<any>;
  dialogOpts: Base.DialogOpts;
  drawerOpts: Base.DrawerOpts;
  loading: Ref<boolean>;
  activeKey: Ref<string>;
  pageStat: Ref<Base.GeneraDataType>;
}) {
  const { searchForm, tableData, drawerOpts, loading, tableRef, dialogOpts, activeKey, pageStat } = state;
  const selectRows = computed<Array<StatementPayable.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  // const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params: Parameters<typeof getListApi>[0] = {
      ...getParams(),
      statusList: [Number(activeKey.value)]
    };
    loading.value = true;
    tableData.value = [];
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const getParams = () => {
    const { agentIdList, companyIdList, payAccountIdList, payTimeList, createTimeList, beforePageNum, pageNum, pageSize } = searchForm;
    const params: any = {
      agentIdList,
      companyIdList,
      payAccountIdList,
      payTimeList: payTimeList?.length ? [dayjs(payTimeList[0]).format("YYYY-MM-DD HH:mm:ss"), dayjs(payTimeList[1]).format("YYYY-MM-DD HH:mm:ss")] : [],
      createTimeList: createTimeList?.length ? [dayjs(createTimeList[0]).format("YYYY-MM-DD HH:mm:ss"), dayjs(createTimeList[1]).format("YYYY-MM-DD HH:mm:ss")] : [],
      beforePageNum,
      pageNum,
      pageSize
    };
    if (payAccountIdList.length) {
      params.payAccountIdList = payAccountIdList.map((t: any) => t[t.length - 1]);
    }
    if (searchForm.sort) {
      params.order = searchForm.order;
      params.sort = searchForm.sort;
    }
    return params;
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi({ ...params }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          1: data[1],
          2: data[2]
        });
        // activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2"]));
      }
    });
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 新增付款流水
  const addPaymentWaterHandle = () => {
    dialogOpts.title = "添加付款流水";
    dialogOpts.width = "520px";
    dialogOpts.component = "AddPaymentWater";
    dialogOpts.passData = {};
    dialogOpts.visible = true;
  };
  // 导入付款流水
  const importPaymentWaterHandle = () => {
    dialogOpts.title = "导入付款流水";
    dialogOpts.width = "520px";
    dialogOpts.component = "ImportWater";
    dialogOpts.passData = {};
    dialogOpts.visible = true;
  };
  // 删除付款流水
  const deletePaymentWaterHandle = (row: PaymentRegistration.ListRows) => {
    Modal.confirm({
      title: "是否删除当前付款流水?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        return deletePaymentWaterApi(row.id).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            searchHandle();
          }
        });
      }
    });
  };
  // 核销费用
  const writeOffFeeHandle = (row: PaymentRegistration.ListRows) => {
    drawerOpts.title = `核销审批单`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.passData = row;
    drawerOpts.component = "WriteOffFee";
    drawerOpts.visible = true;
  };
  //批量导出
  const exportHandle = () => {
    const params = {
      ...getParams(),
      idList: selectRows.value.length ? selectRows.value.map((t: any) => t.id) : undefined,
      statusList: [Number(activeKey.value)]
    };
    return getFinancePaymentSerialExcelListApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!请前往导出中心导出");
        getData();
      }
    });
  };

  return {
    searchHandle,
    getData,
    changeTabHandle,
    selectRows,
    writeOffFeeHandle,
    addPaymentWaterHandle,
    importPaymentWaterHandle,
    deletePaymentWaterHandle,
    exportHandle
  };
}
